package cn.csc.module.product.service.attrcategory;

import cn.csc.framework.common.pojo.PageResult;
import cn.csc.module.product.controller.admin.attrcategory.vo.AttrCategoryCreateReqVO;
import cn.csc.module.product.controller.admin.attrcategory.vo.AttrCategoryExportReqVO;
import cn.csc.module.product.controller.admin.attrcategory.vo.AttrCategoryPageReqVO;
import cn.csc.module.product.controller.admin.attrcategory.vo.AttrCategoryUpdateReqVO;
import cn.csc.module.product.convert.attrcategory.AttrCategoryConvert;
import cn.csc.module.product.dal.dataobject.attrcategory.AttrCategoryDO;
import cn.csc.module.product.dal.dataobject.category.CategoryDO;
import cn.csc.module.product.dal.mysql.attrcategory.AttrCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

import static cn.csc.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.csc.module.product.enums.ErrorCodeConstants.ATTR_CATEGORY_NOT_EXISTS;

/**
 * 属性与分类关联信息 Service 实现类
 *
 * @author liuchuang
 */
@Service
@Validated
public class AttrCategoryServiceImpl implements AttrCategoryService {

    @Resource
    private AttrCategoryMapper attrCategoryMapper;

    @Override
    public BigInteger createAttrCategory(AttrCategoryCreateReqVO createReqVO) {
        // 插入
        AttrCategoryDO attrCategory = AttrCategoryConvert.INSTANCE.convert(createReqVO);
        attrCategoryMapper.insert(attrCategory);
        // 返回
        return attrCategory.getId();
    }

    @Override
    public void updateAttrCategory(AttrCategoryUpdateReqVO updateReqVO) {
        // 校验存在
        validateAttrCategoryExists(updateReqVO.getId());
        // 更新
        AttrCategoryDO updateObj = AttrCategoryConvert.INSTANCE.convert(updateReqVO);
        attrCategoryMapper.updateById(updateObj);
    }

    @Override
    public void deleteAttrCategory(BigInteger id) {
        // 校验存在
        validateAttrCategoryExists(id);
        // 删除
        attrCategoryMapper.deleteById(id);
    }

    private void validateAttrCategoryExists(BigInteger id) {
        if (attrCategoryMapper.selectById(id) == null) {
            throw exception(ATTR_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public AttrCategoryDO getAttrCategory(BigInteger id) {
        return attrCategoryMapper.selectById(id);
    }

    @Override
    public List<AttrCategoryDO> getAttrCategoryList(Collection<BigInteger> ids) {
        return attrCategoryMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AttrCategoryDO> getAttrCategoryPage(AttrCategoryPageReqVO pageReqVO) {
        return attrCategoryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AttrCategoryDO> getAttrCategoryList(AttrCategoryExportReqVO exportReqVO) {
        return attrCategoryMapper.selectList(exportReqVO);
    }

    @Override
    public List<CategoryDO> listByAttrId(BigInteger attrId) {
        return attrCategoryMapper.listByAttrId(attrId);
    }

}
